package com.base.look.act;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.VideoView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.look.R;

//@Route(path = "/look/bigvideo")
public class BigVideoActivity extends Activity implements MediaPlayer.OnPreparedListener {

    private VideoView videoView;
    private ProgressBar process;
    private boolean isLoad = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //定义全屏参数
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //获得当前窗体对象
        Window window = BigVideoActivity.this.getWindow();
        //设置当前窗体为全屏显示
        window.setFlags(flag, flag);
        getWindow().getDecorView().setSystemUiVisibility(View.INVISIBLE);
        setContentView(R.layout.activity_big_video);
        videoView = (VideoView) findViewById(R.id.vv_video);
        process = (ProgressBar) findViewById(R.id.process);
        String videoUrl = getIntent().getStringExtra("videoUrl");
        //设置视频第一帧预览图
        Uri uri = Uri.parse(videoUrl);
        videoView.setVideoURI(uri);
        videoView.setOnPreparedListener(this);
        //MediaController mc = new MediaController(BigVideoActivity.this);//Video是我类名，是你当前的类
        //videoView.setMediaController(mc);//设置VedioView与MediaController相关联
        videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mediaPlayer, int what, int extra) {
                if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
                    process.setVisibility(View.VISIBLE);
                    isLoad = false;
                } else {
                    isLoad = true;
                    process.setVisibility(View.GONE);
                }
                return true;
            }
        });
        videoView.start();
        videoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isLoad){
                    finish();
                }

            }
        });
    }


    @Override
    protected void onPause() {
        super.onPause();
        if (videoView != null) {
            videoView.pause();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (videoView != null) {
            videoView.pause();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (videoView != null) {
            videoView.start();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (videoView != null) {
            videoView.stopPlayback();
        }
    }


    @Override
    public void onPrepared(MediaPlayer mp) {
//        mp.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
//             int currentPosition, duration;
//             public void onBufferingUpdate(MediaPlayer mp, int percent) {
//                                 // 获得当前播放时间和当前视频的长度
//                                 currentPosition = videoView.getCurrentPosition();
//                                 duration = videoView.getDuration();
//                                 int time = ((currentPosition * 100) / duration);
//                                 // 设置进度条的主要进度，表示当前的播放时间
//                                 SeekBar seekBar = (SeekBar)findViewById(R.id.seek);
//                                 seekBar.setProgress(time);
//                                 // 设置进度条的次要进度，表示视频的缓冲进度
//                                 seekBar.setSecondaryProgress(percent);
//                             }
//         });
    }
}
